package main;

import java.awt.Color;
import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import prodotto.*;
import results.*;

public class Esercizio5 {
	
	public static void main(String[] args) {
		
		double s[] = new double[4];
		s[0] = 10;
		s[1] = 0.04;
		s[2] = 0.06;
		s[3] = 0.04;
		
		double v[] = new double[4];
		v[0] = 1.0;
		v[1] = 10.0;
		v[2] = 5.5;
		v[3] = 3.5;
		
		boolean type[] = new boolean[4];
		type[0] = false;
		type[1] = true;
		type[2] = true;
		type[3] = true;
		
		DefaultResult res = null;
		
		int vbsb_index = 0;
		double vbsb = 0.0;
		for(int i=1;i<v.length;i++){
			if((s[i] * v[i]) > vbsb) {
				vbsb = s[i] * v[i];		
				vbsb_index = i;
			}
		}
		/*if(vbsb_index != 1) {
			double temp_v = v[1];
			double temp_s = s[1];
			boolean temp_type = type[1];
			v[1] = v[vbsb_index];
			s[1] = s[vbsb_index];
			type[1] = type[vbsb_index];
			v[vbsb_index] = temp_v;
			s[vbsb_index] = temp_s;
			type[vbsb_index] = temp_type;
		}
		System.out.println("Vb Sv = " + vbsb);
		*/	
		double d = (0.04 * 10) + (0.06 * 5.5) + (0.04 * 3.5);
		double y0_1 = 10 + d;		
		
		try {
			res = (DefaultResult) MVA.mva_load_indipendent_is(v, s, type, 100);
			
		} catch(IllegalArgumentException e) {
			System.out.println(e.toString());
		}
		res.print(1,0);
		
		double temp = ((double)1) / vbsb;
		
		// Create a simple XY chart
        XYSeries series = new XYSeries("Asintoto 1");
        for(int i=0;i<100;i++)			series.add(i,((double)i)/y0_1);
        
		// Create a simple XY chart
        XYSeries series2 = new XYSeries("Asintoto 2");
        for(int i=0;i<100;i++)		series2.add(i,temp);
        
        XYSeries series3 = new XYSeries("Throughput"); 
        for(int i=0;i<100;i++){		series3.add(i,res.get_x(0,i));
        System.out.println(res.get_x(0,i));
        }
        // Add the series to your data set
        XYSeriesCollection dataset = new XYSeriesCollection();
        //XYSeriesCollection dataset2 = new XYSeriesCollection();
        dataset.addSeries(series);
        dataset.addSeries(series2);
        dataset.addSeries(series3);
        // Generate the graph
        JFreeChart chart = ChartFactory.createXYLineChart(
            "Throughput",                // Title
            "number of jobs",                  // x-axis Label
            "throughput",                  // y-axis Label
            dataset,                   // Dataset
            PlotOrientation.VERTICAL,  // Plot Orientation
            true,                      // Show Legend
            true,                      // Use tooltips
            false                      // Configure chart to generate URLs?
            );
        
        XYPlot plot =  (XYPlot) chart.getPlot();
		XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer(); 
		//renderer.setDrawOutlines(true); 
		//renderer.setUseFillPaint(true);
		renderer.setSeriesPaint(0, Color.BLACK);
		renderer.setSeriesPaint(1, Color.RED);
		renderer.setSeriesPaint(2, Color.BLUE);
        
		// Serve per cambiare la scala degli assi
		//NumberAxis domainAxis = new NumberAxis("x");
        //NumberAxis rangeAxis = new LogarithmicAxis("Log(y)");
        //plot.setDomainAxis(domainAxis);
        //plot.setRangeAxis(rangeAxis);
		
		// Serve per esportare il grafico in jpeg
		try {
			ChartUtilities.saveChartAsJPEG(new File("C:\\Users\\fede1587\\eclipseworkspace\\Simulazione\\images\\chart1.jpg"), chart, 500, 300);
	    } catch (IOException e) {
	        System.err.println("Problem occurred creating chart.");
	    }
		
		//res.print();
		
		//GRAFICO DEL TEMPO MEDIO DI ATTESA
		
		// Create a simple XY chart
        XYSeries series4 = new XYSeries("Asintoto 1");
        for(int i=0;i<100;i++)			series4.add(i,d);
        
		// Create a simple XY chart
        XYSeries series5 = new XYSeries("Asintoto 2");
        for(int i=0;i<100;i++)		series5.add(i,(i*vbsb)-10);
        
        XYSeries series6 = new XYSeries("Tempo di risposta"); 
        for(int i=0;i<100;i++){		series6.add(i,(((double)i) / res.get_x(0,i))-10);
        //System.out.println(res.get_x(0,i));
        }
        // Add the series to your data set
        XYSeriesCollection dataset2 = new XYSeriesCollection();
        //XYSeriesCollection dataset2 = new XYSeriesCollection();
        dataset2.addSeries(series4);
        dataset2.addSeries(series5);
        dataset2.addSeries(series6);
        // Generate the graph
        JFreeChart chart2 = ChartFactory.createXYLineChart(
            "Tempo di risposta",                // Title
            "number of jobs",                  // x-axis Label
            "tempo di risposta",                  // y-axis Label
            dataset2,                   // Dataset
            PlotOrientation.VERTICAL,  // Plot Orientation
            true,                      // Show Legend
            true,                      // Use tooltips
            false                      // Configure chart to generate URLs?
            );
        
        XYPlot plot2 =  (XYPlot) chart2.getPlot();
		XYLineAndShapeRenderer renderer2 = (XYLineAndShapeRenderer) plot2.getRenderer(); 
		//renderer.setDrawOutlines(true); 
		//renderer.setUseFillPaint(true);
		renderer2.setSeriesPaint(0, Color.BLACK);
		renderer2.setSeriesPaint(1, Color.RED);
		renderer2.setSeriesPaint(2, Color.BLUE);
        
		// Serve per cambiare la scala degli assi
		//NumberAxis domainAxis = new NumberAxis("x");
        //NumberAxis rangeAxis = new LogarithmicAxis("Log(y)");
        //plot.setDomainAxis(domainAxis);
        //plot.setRangeAxis(rangeAxis);
		
		// Serve per esportare il grafico in jpeg
		try {
			ChartUtilities.saveChartAsJPEG(new File("C:\\Users\\fede1587\\eclipseworkspace\\Simulazione\\images\\chart2.jpg"), chart2, 500, 300);
	    } catch (IOException e) {
	        System.err.println("Problem occurred creating chart.");
	    }
		
		
		
		
	}

}
